<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Logic;


use App\Exceptions\ApiException;
use App\Http\Controllers\Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Monolog\Handler\IFTTTHandler;

class ActivityLogic extends Logic
{

    /**
     * 解析规则发送奖品
     * @param $arr
     * @param $uid
     * @param $name
     * @return array
     * @throws ApiException
     */
    public function analysisHandle($arr, $uid, $name)
    {
        $arr  = explode('|', $arr);
        $base = app(Base::class);
        DB::beginTransaction();
        try {
            $data = [];
            foreach ($arr as $v) {
                $v_arr = explode(':', $v);
                switch ($v_arr[0]) {
                    case 'diamond':
                        $base->grant_diamonds($uid, $v_arr[1], ":{$name}赠送钻石", "diamond:{$v_arr[1]}");
                        $data['diamond'] = (int)$v_arr[1];
                        break;
                    case 'room_card':
                        $base->grant_room_ticket($uid, $v_arr[1]);
                        $data['room_card'] = (int)$v_arr[1];
                        break;
                    case 'gold':
                        $base->grant_golds($uid, $v_arr[1], "{$name}赠送金币", "gold:{$v_arr[1]}");
                        $data['gold'] = (int)$v_arr[1];
                        break;
                    case 'rotary_count':
                        $rotary_count = (int)$v_arr[1] + LuckyDrawLogic::instance()->ini_count;
                        Redis::hSet('LUCKY_DRAW_COUNT', $uid, $rotary_count);
                        Redis::expire('LUCKY_DRAW_COUNT', strtotime(date('Y-m-d', strtotime('+1 day'))));
                        $data['rotary_count'] = $rotary_count;
                        break;
                    case 'quickly_grab_ticket':
                        $base->grant_quickly_grab_ticket($uid, $v_arr[1]);
                        $data['quickly_grab_ticket'] = (int)$v_arr[1];
                        break;
                    //签到时送快抢赛次数
                    //需要判断快抢赛表是否有数据,有数据就直接更新
                    //否侧就直接更新次数 1(普通用户送的次数)+送的快抢赛次数
                    case 'sign_quickly_grab_ticket':
                        $object = DB::table('t_quickly_grab_ticket_info')->where('uid', $uid)->first();
                        $return_count = $v_arr[1];
                        if ($object) {
                            if ($object->date == date('Y-m-d')) {
                                $count = $object->count + $v_arr[1];
                            } else {
                                $count = 1 + $v_arr[1];
                            }
                            DB::table('t_quickly_grab_ticket_info')->where('uid', $uid)->update(['count' => $count]);
                        } else {
                            $count = 1 + $v_arr[1];
                            DB::table('t_quickly_grab_ticket_info')->insert([
                                'uid' => $uid,
                                'count' => $count,
                                'date' => date('Y-m-d')
                            ]);
                        }
                        $data['quickly_grab_ticket'] = (int)$return_count;
                        break;

                }
            }
            DB::commit();
            return $data;
        } catch (\Exception $exception) {
            throw new ApiException('奖励活动错误' . $exception->getMessage());
        }
    }


    /**
     * 根据类型获取奖品规则
     * @param $type
     * @return bool|\Illuminate\Database\Eloquent\Model|null|object|static
     * @throws ApiException
     */
    public function activityConfInfo($type)
    {
        $object = DB::table('t_item_activity_conf')->where('type', $type)->where('status', 1)->orderBy('priority', 'desc')->first();
        if (empty($object)) {
            throw new ApiException('奖励活动数据不存在');
        }
        if (!empty($object->start_time)) {
            if (time() < strtotime($object->start_time)) {
                return false;
            }
        }
        if (!empty($object->end_time)) {
            if (time() > strtotime($object->end_time)) {
                return false;
            }
        }
        return $object;
    }


    /**
     * 解析规则成文字
     * @param $rule
     * @return mixed
     */
    public function ruleContent($rule)
    {
        if (empty($rule)) {
            return '';
        }
        $mapping  = ['diamond' => '钻石', 'room_card' => '房卡', 'gold' => '金币', 'quickly_grab_ticket' => '快抢赛次数', 'sign_quickly_grab_ticket' => '快抢赛次数'];
        $rule_arr = explode('|', $rule);
        $content  = '';
        foreach ($rule_arr as $value) {
            $var_arr = explode(':', $value);
            //配置奖品不为0才返回文案内容
            if (!empty($var_arr[1])) {
                $content .= $mapping[$var_arr[0]] . ':' . $var_arr[1] . ',';
            }
        }
        return $content;
    }

}